package com.ruoyi.web.controller.shopping.order;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.AddressService;
import com.ruoyi.system.service.OrderService;
import com.ruoyi.system.service.ProductService;
import com.ruoyi.system.service.ShoppingCartService;
import com.ruoyi.system.view.common.R;
import com.ruoyi.web.controller.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/xiaomi/v2/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private AddressService addressService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private ProductService productService;

    @PostMapping("/create")
    public R<String> createOrder(@RequestParam(value = "addressId")Integer addressId,
                                 @RequestParam(value = "shoppingCartIds") Integer[] shoppingCartIds,
                                 HttpServletRequest request){

        if (addressId == -1){
            for (Integer shoppingCartId : shoppingCartIds) {
//                shoppingCartService.deleteByShoppingCartId(shoppingCartId);
            }
            return R.error("请选择地址");
        }
        OrderT order = new OrderT();
        order.setAddressId(addressId);
        order.setOrderNum(UUID.randomUUID().toString());
        order.setOrderTime(LocalDateTime.now());
        order.setOrderDtlId(UUID.randomUUID().hashCode());
        String s = TokenUtil.userIdTokenCheck(request);
        order.setUserId(Integer.valueOf(s));
        for (Integer shoppingCartId : shoppingCartIds) {
            if (order.getShoppingCartIds() == null){
                order.setShoppingCartIds(shoppingCartId.toString()+",");
            }else{
                order.setShoppingCartIds(order.getShoppingCartIds()+shoppingCartId.toString()+",");
            }
            shoppingCartService.updateDelete(shoppingCartId,1);
        }
        System.out.println(order.getShoppingCartIds());
        orderService.insertOrder(order);
        return R.success("生成订单成功");
    }

    @GetMapping
    public R<List<OrderDDT>> getOrders(HttpServletRequest request){
        String s = TokenUtil.userIdTokenCheck(request);
        //获取当前用户的全部订单号
        List<OrderT> all = orderService.getAll(Integer.valueOf(s));
        //一级目录
        ArrayList<OrderDDT> orderDDTS = new ArrayList<OrderDDT>();
        for (OrderT orderT : all) {
            Integer orderId = orderT.getOrderId();
            //根据订单号查找当前用户下每个订单的购物车编号
            OrderT allById = orderService.getAllById(Integer.valueOf(s), orderId);
            String shoppingCartIds = allById.getShoppingCartIds();
            String subString = shoppingCartIds.substring(0,shoppingCartIds.length()-1);
            String[] result = subString.split(",");
            //二级目录
            OrderDDT orderDDT = new OrderDDT();
            //三级
            ArrayList<OrderDto> orderDtos = new ArrayList<OrderDto>();
            for (String s1 : result) {
                System.out.println(orderId +":"+s1);
                //根据商品id获取商品信息
                ShoppingCart byShoppingCartId = shoppingCartService.getByShoppingCartId1(Integer.valueOf(s1));
                System.out.println(byShoppingCartId);
                //根据OrderDto的需求提取商品信息
                OrderDto orderDto = new OrderDto();
                orderDto.setNum(byShoppingCartId.getNum());//y
                orderDto.setOrderDtlId(1111);//n
                orderDto.setProductId(byShoppingCartId.getProductId());//y
                orderDto.setProductName(productService.getAll(null,byShoppingCartId.getProductId(),null).getProductName());//y
                orderDto.setProductPicture(productService.getAll(null,byShoppingCartId.getProductId(),null).getProductPicture());//y
                orderDto.setProductSellingPrice(productService.getAll(null,byShoppingCartId.getProductId(),null).getProductSellingPrice());//y
                orderDtos.add(orderDto);
            }
            orderDDT.setProducts(orderDtos);

            //获取地址表的id
            Integer addressId = orderT.getAddressId();
            //根据地址id查询地址信息
            Address byaAddressId = addressService.getByaAddressId(addressId);
//            System.out.println("地址信息:"+byaAddressId);
            //根据orderDDT的需求提取地址信息
            orderDDT.setAddressDtl(byaAddressId.getAddressDtl());
            orderDDT.setAddressTags(byaAddressId.getAddressTags());
            orderDDT.setAddressee(byaAddressId.getName());
            orderDDT.setAddressArea(byaAddressId.getArea());
            orderDDT.setAddressPhone(byaAddressId.getPhone());
            orderDDT.setOrderId(orderId);
            orderDDT.setOrderNum(UUID.randomUUID().toString());
            orderDDT.setOrderTime(byaAddressId.getCreateTime());
            orderDDTS.add(orderDDT);
        }
        return R.success(orderDDTS);
    }
}
